package com.wanxi.servlet.user;

import com.alibaba.fastjson.JSONObject;
import com.wanxi.VO.UserVO;
import com.wanxi.dto.ResultDTO;
import com.wanxi.service.UserService;
import com.wanxi.service.impl.UserServiceImpl;
import com.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : 林子淳
 * @create 2023/2/24 10:00
 */
//@WebServlet("/back/user/findAll")
public class UserFindAllServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //2、获取前端传递后端的值
        String userName = req.getParameter("username");
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String enable = req.getParameter("enable");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String hobby = req.getParameter("hobby");
        String startBirthday = req.getParameter("start");
        String endBirthday = req.getParameter("end");
        //3.封装VO数据
        UserVO userVO = new UserVO();
        userVO.setPage(Tool.strToPage(page));
        userVO.setLimit(Tool.strToLimit(limit));
        userVO.setStartBirthday(Tool.nullToStr(startBirthday));
        userVO.setEndBirthday(Tool.nullToStr(endBirthday));
        userVO.setHobby(Tool.nullToStr(hobby));
        userVO.setSex(Tool.nullToStr(sex));
        userVO.setUsername(Tool.nullToStr(userName));
        userVO.setEmail(Tool.nullToStr(email));
        userVO.setEnable(Tool.strToInt(enable));
//        System.out.println("进入的了页面："+userVO.toString());
        //        4.调用服务逻辑层
        UserService userService = new UserServiceImpl();
        //        5.得到返回的结果
//        ResultDTO resultDTO = null;
        ResultDTO resultDTO = userService.findAll(userVO);
        //        6.返回给前端
        resp.getWriter().println(JSONObject.toJSONString(resultDTO));
    }
}
